package cardsbody
{
	import Classes.pi_events;
	
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import editor.pi_cardbody;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class pi_pngcard extends pi_cardbody {
		protected var _maskPhoto: uif_component;
		protected var _label: uif_label;
		protected var _bitmap: Bitmap;
		
		public function pi_pngcard(parent:uif_component, 
									ax:int=0, ay:int=0,
									awidth: int=0, aheight: int=0, styleName: String='') {
			super(parent, ax, ay, awidth, aheight, styleName);
			_maskPhoto 	= new uif_component(this, 0, 0, awidth, aheight, 'white');
			_label		= new uif_label(this, 0, 0, awidth, aheight, 'maskHelp');
			_label.visible = false;
			_photo.mask = _maskPhoto;
			_photo.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e: Event): void {
				_label.visible = false;
			});
		}
		
		private function photoToCenter(): void {
			_photo.x = _maskPhoto.x + (_maskPhoto.width - _photo.width) / 2;
			_photo.y = _maskPhoto.y + (_maskPhoto.height - _photo.height) / 2;
		}
		
		override public function infoFromCollector(): String {
			if (bmpContent()) {
				var scale: Number = (Math.round(_photo.scaleX / _mask.source.scaleX * 100) / 100);
				var flips: Point = (app as pi_app).getEvent(pi_events.GET_FLIPS, pi_events.FLIPSDATA);
				
				var center: Point = new Point(_maskInfo.pos.width / 2, _maskInfo.pos.height / 2);
				var mat: Matrix = _photoMatrix.clone();
				mat.invert();
				var pos2: Point = mat.transformPoint(center);
				
				return _maskInfo.toString() + 
					'&photo_pos2=' + pos2.x  + ',' + pos2.y +
					'&scale=' + scale +
					(flips?('&flips=' + flips.x + ',' + flips.y):'&flips=1,1') + 
					'&rotation=' + _photo.rotation +
					'&img=' + _photo.sourceUrl.replace('http://', '');
			} else return super.infoFromCollector();
		}
		
		override public function applyPhotoMatrix(matrix: Matrix): void {
			super.applyPhotoMatrix(matrix);
			if (matrix) {
				matrix = matrix.clone();
				matrix.translate(_maskInfo.pos.x, _maskInfo.pos.y);
				matrix.scale(_mask.source.scaleX, _mask.source.scaleY);
				matrix.translate(_mask.source.x, _mask.source.y);
				_photo.transform.matrix = matrix;
			} else photoToCenter();
		}
		
		override protected function doLoadMask(e: Event): void {
			var rect: Rectangle = new Rectangle();
			rect.topLeft = _mask.source.transform.matrix.transformPoint(_maskInfo.pos.topLeft);
			rect.bottomRight = _mask.source.transform.matrix.transformPoint(_maskInfo.pos.bottomRight);
			_maskPhoto.coordinates = rect;
			_label.coordinates = rect;
			if (card_photo.contentLoaderInfo.bytesLoaded == 0) {
				_label.visible = true;
				_label.text = pi_data.SELECTPHOTOHELP;
			}
			super.doLoadMask(e);
		}
	}
}